#include "stdafx.h"
#include "bridge.h"
#include "OgreNewt_Joint.h"

using namespace Ogre;

int Bridge::counter=0;

Bridge::Bridge(Ogre::SceneManager *mSceneM, OgreNewt::World *W, Ogre::Vector3 position, int parts,Real scale)
{

			mSceneMgr=mSceneM;
			mWorld=W;
			OgreNewt::Body* parent;

			Entity* ropeEnd = mSceneMgr->createEntity(  "plank"+Ogre::StringConverter::toString(counter++), "bridgePlank.mesh" );
			OgreNewt::ConvexCollisionPtr col = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::ConvexHull(mWorld,ropeEnd,1));
			ropeEnd->setMaterialName("murbob4");
			OgreNewt::Body* ropeB = new OgreNewt::Body( mWorld, col );
			SceneNode* rNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
			rNode->attachObject( ropeEnd );
			Ogre::Vector3 inertia, offset;
			col->calculateInertialMatrix(inertia, offset);

			ropeB->setMassMatrix( 0, inertia );
            ropeB->setCenterOfMass(offset);
			ropeB->setPositionOrientation( position, Ogre::Quaternion::IDENTITY );
			ropeB->attachNode(rNode );

			parent=ropeB;

			for(int i=0;i<parts;i++)
			{

			Entity* ropeE = mSceneMgr->createEntity(  "plank"+Ogre::StringConverter::toString(counter++), "bridgePlank.mesh" );
			ropeE->setMaterialName("murbob4");
			SceneNode* rNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
			rNode->attachObject( ropeE );
			//rNode->setScale(Vector3(scale));
			//rNode->_updateBounds();//showBoundingBox(true);
			col = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::ConvexHull(mWorld,ropeE,1));
			//rNode->setScale(Vector3(2,scale,2));
			//ropeE->setMaterialName("ropeM");
			ropeB = new OgreNewt::Body( mWorld, col );

			Ogre::Vector3 inertia, offset;
			 col->calculateInertialMatrix(inertia, offset);

			 if(i==parts-1)
			ropeB->setMassMatrix( 0, inertia );
			 else
			 ropeB->setMassMatrix( 0.1, inertia );

            ropeB->setCenterOfMass(offset);
			ropeB->setPositionOrientation( position+Vector3(0,0,scale*(1.8+i*1.8)), Ogre::Quaternion::IDENTITY );
			ropeB->attachNode(rNode );

			ropeB->setCustomForceAndTorqueCallback<Bridge>(&Bridge::move_callback, this);
			ropeB->setAngularDamping(Vector3(1,1,1)*0.6);
			ropeB->setLinearDamping(0.6);
			//ropeB->setType(3);

			OgreNewt::BallAndSocket* b=new OgreNewt::BallAndSocket(parent,ropeB,position+Vector3(0,0,scale*(0.9+i*1.8)),30);
			b->setCollisionState(0);
			
			//b->setStiffness(0.5);

			//b=new OgreNewt::BallAndSocket(parent,ropeB,position+Vector3(-2,0,scale*(0.9+i*1.8)),30);
			//b->setCollisionState(0);

			parent=ropeB;

			if(i==parts-1)
				parent->setPositionOrientation( position+Vector3(0,4,scale*(1.8+i*1.8)-2), Ogre::Quaternion::IDENTITY );

			}




		
}

void Bridge::move_callback(OgreNewt::Body* me, float timeStep, int threadIndex )
{

	me->addForce(Ogre::Vector3(0,-1,0));
	me->setOmega(me->getOmega()+0.01*Ogre::Vector3(0,0,-me->getOrientation().xAxis().y));

}

void Bridge::move_callback2(OgreNewt::Body* me, float timeStep, int threadIndex )
{

	me->addForce(Ogre::Vector3(-3,-1,0));
	me->setOmega(me->getOmega()+0.01*Ogre::Vector3(0,0,-me->getOrientation().xAxis().y));

}

Bridge::~Bridge () {
 
}
